package com.prtek.reminder.activity;

import android.app.AlertDialog;
import android.content.Intent;
import android.database.ContentObserver;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;
import butterknife.BindView;
import butterknife.OnClick;
import com.prtek.iis.grammar.Expression;
import com.prtek.iis.grammar.JoinExpressionGroup;
import com.prtek.iis.grammar.OrExpressionGroup;
import com.prtek.reminder.Constant;
import com.prtek.reminder.R;
import com.prtek.reminder.adapter.MessageAdapter;
import com.prtek.reminder.bean.Notes;
import com.prtek.reminder.helper.UpgradeHelper;
import com.prtek.reminder.listener.OnItemClickListener;
import com.prtek.reminder.listener.OnMenuKeyEventListenter;
import com.prtek.reminder.model.MainModel;
import com.prtek.reminder.model.SpeechEventModelImpl;
import com.prtek.reminder.presenter.IMainPresenter;
import com.prtek.reminder.presenter.MainPresenterImpl;
import com.prtek.reminder.util.AudioPlayer;
import com.prtek.reminder.view.MainView;
import java.util.List;

public class MainActivity extends BaseActivity implements OnMenuKeyEventListenter, MainView {

  @BindView(R.id.msg_rv) RecyclerView mMessageRv;
  @BindView(R.id.msg_add) TextView mMsgAdd;
  @BindView(R.id.planned_date_rv) RecyclerView mPlannedDateRv;
  @BindView(R.id.date_tip_tv) TextView mDateTipTv;

  private MessageAdapter mMessageAdapter;
  private MessageAdapter mPlannedAdapter;
  private IMainPresenter mMainPresenter;
  private AudioPlayer mAudioPlayer;

  @Override protected void init() {
    registerObserver(true);
    mMessageAdapter = new MessageAdapter();
    mPlannedAdapter = new MessageAdapter();
    mMessageAdapter.setMode(Constant.MODE_MESSAGE);
    mPlannedAdapter.setMode(Constant.MODE_EXPIRE);
    initRecycleView(mMessageRv, mMessageAdapter);
    initRecycleView(mPlannedDateRv, mPlannedAdapter);
    mMainPresenter = new MainPresenterImpl(new MainModel(), new SpeechEventModelImpl());
    mMainPresenter.setupSpeechEventListener(this, getExpression());
    mPlannedAdapter.setOnItemClickListener(onItemClickListener);
    mMessageAdapter.setOnItemClickListener(onItemClickListener);
  }



  final OnItemClickListener onItemClickListener = new OnItemClickListener() {
    @Override public void onItemClick(Drawable drawable, String path) {
      if (mAudioPlayer == null) {
        mAudioPlayer = AudioPlayer.getInstance();
      }

      mAudioPlayer.startPlayAudio(drawable, path);
    }
  };

  @Override protected void initData() {
    mMainPresenter.attachView(this);
  }

  @Override protected int getLayoutId() {
    return R.layout.activity_main;
  }

  @Override protected Expression getExpression() {
    return new JoinExpressionGroup().add(new OrExpressionGroup().add(getString(R.string.add_msg)));
  }

  @OnClick(R.id.msg_add) public void onViewClicked() {
    startEditActivity();
  }

  final ContentObserver mObserver = new ContentObserver(null) {
    @Override public void onChange(boolean selfChange) {
      super.onChange(selfChange);
      mMessageRv.post(() -> mMainPresenter.refresh());
    }
  };

  @Override public void onMenuKeyEvent(final Notes notes) {
    AlertDialog.Builder deleteDialog = new AlertDialog.Builder(this);
    deleteDialog.setTitle(R.string.title)
        .setMessage(R.string.message)
        .setPositiveButton(R.string.pickerview_submit, (dialog, i) -> {
          mMainPresenter.deleteNotes(notes);
          mContext.getContentResolver().notifyChange(UpgradeHelper.CONTENT_URI, null);
          dialog.dismiss();
        })
        .setNegativeButton(R.string.pickerview_cancel, (dialog, i) -> {
        })
        .create()
        .show();
  }

  @Override public void setItemDate(List<Notes> createdNotes, List<Notes> expireNotes) {
    mMessageAdapter.setNotesList(createdNotes);
    mPlannedAdapter.setNotesList(expireNotes);
    mDateTipTv.setVisibility(expireNotes.isEmpty() ? View.VISIBLE : View.GONE);
  }

  @Override public void remindCreate() {
    startEditActivity();
  }

  @Override protected void onStop() {
    super.onStop();
    if (mAudioPlayer != null) {
      mAudioPlayer.stop();
    }
    mMainPresenter.removeSpeechListener();
  }

  @Override protected void onDestroy() {
    super.onDestroy();
    mMainPresenter.onDestroy();
    registerObserver(false);
  }

  private void initRecycleView(RecyclerView recyclerView, MessageAdapter adapter) {
    final DividerItemDecoration divider =
        new DividerItemDecoration(this, DividerItemDecoration.VERTICAL);
    divider.setDrawable(getDrawable(R.drawable.item_divider_color));
    recyclerView.setLayoutManager(new LinearLayoutManager(this));
    recyclerView.setHasFixedSize(true);
    recyclerView.addItemDecoration(divider);
    recyclerView.setAdapter(adapter);
    adapter.setOnMenuKeyEventListenter(this);
  }

  private void registerObserver(boolean register) {
    if (register) {
      getContentResolver().registerContentObserver(UpgradeHelper.CONTENT_URI, true, mObserver);
    } else {
      getContentResolver().unregisterContentObserver(mObserver);
    }
  }

  private void startEditActivity() {
    Intent intent = new Intent(this, RemindEditActivity.class);
    startActivity(intent);
  }
}
